import { Storage } from './utils';
import axios from 'axios';

//访问.env文件定义的环境变量
axios.defaults.baseURL = process.env.VUE_APP_API_BASE_URL;

//拦截器请求
axios.interceptors.request.use(
  (config) => {
    //从localStorage中获取访问token
    const accessToken = Storage.get('access_token');
    if (accessToken) {
      //发送请求时给请求头新增携带token信息的Authorization
      config.headers.Authorization = `Bearer ${accessToken}`;
    }
    return config;
  },
  (err) => Promise.reject(err)
);

//响应拦截
axios.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (err) => Promise.reject(err)
);

export default axios;
